/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session09;

import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author sinhnx <sinhnx@fpt.aptech.ac.vn>
 */
public class GmailSender {
    
    public static void main(String[] args) {
        final String userName = "s0s999999999@gmail.com";
        final String password = "fptaptech";
        
        Properties props = new Properties();
        
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
        
        Session session = Session.getInstance(props, new Authenticator() {
            
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password);
            }
        });
        
        Message msg = new MimeMessage(session);
        try {
            msg.setFrom(new InternetAddress(userName));
            msg.setRecipient(Message.RecipientType.TO, new InternetAddress("sinhnx@fpt.aptech.ac.vn"));
            
            msg.setSubject("Test mail");
            msg.setText("Test java mail");
            
            Transport.send(msg);
        } catch (MessagingException ex) {
            Logger.getLogger(GmailSender.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        
    }
}
